package com.roncoo.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.roncoo.permission.dao.AdminMapper;
import com.roncoo.permission.entity.Admin;
import com.roncoo.permission.entity.AdminroleTMenu;
import com.roncoo.permission.entity.Message;
import com.roncoo.permission.entity.TMenu;
import com.roncoo.permission.service.IAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yuechuanhao
 * @since 2021-03-23
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private AdminMapper adminMapper;

    @Override
    public IPage findAllPage(Admin admin, Page page) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.like("phone", admin.getPhone());
        Page page1 = adminMapper.selectPage(page, adminQueryWrapper);
        return page1;
    }

    @Override
    public List<String> findMenuByRoleid(Integer roleid) {
        //放 菜单
        List<String> list = new ArrayList<>();
        List<TMenu> menuByRoleid = adminMapper.findMenuByRoleid(roleid);
        if(menuByRoleid.size()!=0){
            for(TMenu tMenu : menuByRoleid){
                list.add(tMenu.getMurl());
            }
        }
        return list;
    }

    @Override
    public Message bLogin(Admin admin) {
        //1.获取当前登录的用户
        Subject subject = SecurityUtils.getSubject();
        //2.认证
        UsernamePasswordToken token=new UsernamePasswordToken(admin.getPhone(),admin.getPassworld());
        Message message=new Message();

        try{
            subject.login(token);
            //拿到正确的实体
            admin=(Admin) subject.getPrincipal();
        }catch (UnknownAccountException e){
            System.out.println("用户名不对！！！！");
            //重新跳转到登录页面
            message.setCode(1);
            message.setStatu(false);
            message.setMsg("用户名不对！！！");
            return message;
        }catch(IncorrectCredentialsException e){
            System.out.println("密码不对！！！！");
            //重新跳转到登录页面
            message.setCode(1);
            message.setStatu(false);
            message.setMsg("密码不对！！！");
            return message;
        }
        if(admin.getState()==1){
            System.out.println("已经被禁用！！！！");
            //重新跳转到登录页面
            message.setCode(1);
            message.setStatu(false);
            message.setMsg("已经被禁用！！！");
            return message;
        }
        message.setCode(0);
        Map map=new HashMap<>();
        map.put("admin",admin);
        message.setObj(map);
        message.setStatu(true);
        message.setMsg("登陆成功！");
        return message;
    }

    @Override
    public boolean findPeople(Integer roleid) {
        List<Map> people = adminMapper.findPeople(roleid);
        if(people.size()>0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean findrole(Integer id) {
        int a =  adminMapper.findrole(id);
        if(a==1||a==2){
            return false;
        }else {
            return true;
        }
    }


}
